/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package archive;

import ejb.archive.Allegato;
import ejb.archive.AllegatoFacadeRemote;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.upload.FormFile;
import system.Utilities;

/**
 *
 * @author ciummo
 */
public class AnnexAction extends DispatchAction {

    /*
     * forward name="success" path=""
     */
    private final static String SUCCESSORDER = "successOrder";
    private final static String FAILUPLOAD = "failOrder";
    

    private final static String DESTINATION = "/attachment/";
    
    
    private Utilities utility = new Utilities();
    
    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1, where "method" is the value
     * specified in <action> element : ( <action parameter="method" .../> )
     */
    public ActionForward upload(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        
        String type = request.getParameter("type");
        
        String idReference = request.getSession().getAttribute("idOrder").toString();
        
        System.out.println(idReference);
        
        AnnexActionForm annexForm = (AnnexActionForm) form;

        FormFile newFile = annexForm.getFile();
        String filename = newFile.getFileName();
        //TODO : Controllare l'esistenza di un file con lo stesso nome
        
        
        //TODO : dove salvare ? 
        String path = this.getServlet().getServletContext().getRealPath(DESTINATION + filename);
        
        FileOutputStream fout = new FileOutputStream(path);
        
        annexForm.setFilename(filename);
        
        try {
            fout.write(newFile.getFileData());
            fout.close();
        } catch (Exception ex) {
            fout.close();
            ex.printStackTrace();
            return mapping.findForward(FAILUPLOAD);
        }
        
        AllegatoFacadeRemote allegatoFacade = (AllegatoFacadeRemote) utility.callRemoteBean("AllegatoFacade/remote");
        
        Allegato allegato = new Allegato ();
        allegato.setIdRiferimento(idReference);
        allegato.setNomeFile(filename);
        allegato.setTipo(type);
        
        allegatoFacade.create(allegato);
        
        System.out.println(idReference);
        
        request.getSession().setAttribute("id", idReference);
        
        return mapping.findForward(SUCCESSORDER);
        
    }
    
        public ActionForward download(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            
        
            int id = Integer.parseInt(request.getParameter("idAnnex"));
            
            
            AllegatoFacadeRemote allegatoFacade = (AllegatoFacadeRemote) utility.callRemoteBean("AllegatoFacade/remote");
            
            Allegato allegato = allegatoFacade.find(id);
            
            String path = this.getServlet().getServletContext().getRealPath(DESTINATION + allegato.getNomeFile());
            
            
            FileInputStream inputStream = new FileInputStream(path);

            response.setHeader("Content-Disposition","attachment; filename="+allegato.getNomeFile());
            try {
                int c;
                while ((c = inputStream.read()) != -1) {
                response.getWriter().write(c);
                }
            } finally {
                if (inputStream != null) 
                    inputStream.close();
                    response.getWriter().close();
            }

            return mapping.findForward(SUCCESSORDER);

            }
        
        
        public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            
            int id = Integer.parseInt(request.getParameter("idAnnex"));
            
            AllegatoFacadeRemote allegatoFacade = (AllegatoFacadeRemote) utility.callRemoteBean("AllegatoFacade/remote");
            
            allegatoFacade.remove(allegatoFacade.find(id));
            
            
            return mapping.findForward(SUCCESSORDER);
        }        
        

}
